package lt.marsrutai.mobile.android.util;

import lt.marsrutai.mobile.android.AppContext;
import lt.marsrutai.mobile.android.model.Bookmark;
import android.view.View;
import android.view.View.OnClickListener;

/**
 * Sets up bookmark controls and their behavior 
 * 
 * @author Justas
 */
public class BookmarkControlManager {
    
    public static <T extends Bookmark> void setUpControls(final View starButtonOn, final View starButtonOff, final BookmarkInfoProvider<T> provider) {
        starButtonOff.setFocusable(false);
        starButtonOff.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                starButtonOff.setVisibility(View.GONE);
                starButtonOn.setVisibility(View.VISIBLE);

                AppContext.getBookmarkService().add(provider.getBookmark());
            }
        });

        starButtonOn.setFocusable(false);
        starButtonOn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                starButtonOn.setVisibility(View.GONE);
                starButtonOff.setVisibility(View.VISIBLE);

                AppContext.getBookmarkService().remove(provider.getBookmark());
            }
        });

        if (AppContext.getBookmarkService().exists(provider.getBookmark())) {
            starButtonOff.setVisibility(View.GONE);
            starButtonOn.setVisibility(View.VISIBLE);
        }
        else {
            starButtonOn.setVisibility(View.GONE);
            starButtonOff.setVisibility(View.VISIBLE);
        }
    }

    public static void setUpControls(final View starButtonOn, final View starButtonOff, final Bookmark bookmark) {
        starButtonOff.setFocusable(false);
        starButtonOff.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                starButtonOff.setVisibility(View.GONE);
                starButtonOn.setVisibility(View.VISIBLE);

                AppContext.getBookmarkService().add(bookmark);
            }
        });

        starButtonOn.setFocusable(false);
        starButtonOn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                starButtonOn.setVisibility(View.GONE);
                starButtonOff.setVisibility(View.VISIBLE);

                AppContext.getBookmarkService().remove(bookmark);
            }
        });

        if (AppContext.getBookmarkService().exists(bookmark)) {
            starButtonOff.setVisibility(View.GONE);
            starButtonOn.setVisibility(View.VISIBLE);
        }
        else {
            starButtonOn.setVisibility(View.GONE);
            starButtonOff.setVisibility(View.VISIBLE);
        }
    }
    
    public interface BookmarkInfoProvider<T extends Bookmark> {
        
        T getBookmark();
    }
}
